<template>
  <div class="login">
    <div class="gaizu" @click="$store.commit('Yinchan', false)"></div>
    <div class="login-box">
      <!-- 点击删除 -->
      <div class="close" @click="$store.commit('Yinchan', false)">
        <img src="../../assets/images/login/userArrow.png" alt="" />
      </div>
      <ul class="login-type flex">
        <li :class="{ active: currentIndex === 0 }" @click="currentIndex = 0">
          手机号码登录
        </li>
        <li :class="{ active: currentIndex === 1 }" @click="weixinClickFn">
          微信扫码登录
        </li>
      </ul>
      <div v-show="currentIndex === 0" class="phone-login">
        <div class="form-item">
          <input type="text" placeholder="输入手机号" v-model="phone" />
        </div>
        <div class="form-item">
          <slide-verify
            class="slide-box"
            :l="60"
            :r="10"
            :w="395"
            :h="135"
            :slider-text="msg"
            @success="onSuccess"
            @fail="onFail"
            @refresh="onRefresh"
          ></slide-verify>
        </div>
        <div class="form-item">
          <input type="text" placeholder="输入验证码" v-model="yanzenma" />
          <span class="btn" @click="getVeriryCode"
            >获取验证码<span v-show="time !== 60">({{ time }})</span>
          </span>
        </div>
        <div class="form-item">
          <div class="btn big" @click="phoneLogin">登录</div>
        </div>
      </div>
      <div v-show="currentIndex === 1" class="wx-login">
        <div id="weixin"></div>
      </div>
    </div>
  </div>
</template>
 
<script>
import axios from "axios";
// import { valdateTelephone } from "../../utils/index";
import { toas } from "../../utils/toas.js";
import { getVerifyApi, phoneLoginApi } from "../http/api";
export default {
  data() {
    return {
      sojihao: "", //输入的手机号
      yanzenma: "", //验证码
      currentIndex: 0,
      msg: "向右滑动",
      time: 60,
      phone: "",
      // 是否已经滑动验证成功
      isSlideVerfiy: false,
      currentIndex: 0,
    };
  },
  methods: {
    // 点击了微信扫码登录
    weixinClickFn() {
      this.currentIndex = 1;
      let _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa(process.env.VUE_APP_STATE_URL + _this.$route.path)
        ),
        // 调用样式文件
        href: "",
      });
    },
    //登入
    async phoneLogin() {
      if (this.phone.trim() === "") return toas("请先输入手机号", "dander");
      //拖动验证
      if (!this.isSlideVerfiy) return toas("请拖动验证码", "dander");
      if (this.yanzenma.trim() === "") return toas("请输入验证码", "dander");
      // phoneLoginApi().then();

      const res = await phoneLoginApi({
        code: this.yanzenma,
        phone: this.phone,
      });
      console.log(res);
      if (res.code === 0) {
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        //eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2M…WIifQ.tDPBcsQXjIer_ORr1x_NgTR4guhpPOzA9hz7Dw3dp-Y
        //提升登入成功
        toas(res.message, "success");
        //吧login隐藏
        this.$store.commit("Yinchan", false);
      }
    },
    //点击验证码框
    getVeriryCode() {
      if (this.phone.trim() === "") return toas("请先输入手机号", "dander");
      //拖动验证
      if (!this.isSlideVerfiy) return toas("请拖动验证码", "dander");
      //点击发送验证码倒计时
      if (this.time !== 60) return toas("验证码已经发送，别点了", "dander");
      //发送请求
      //倒计时
      this.time--;
      const timer = setInterval(() => {
        this.time--;
        if (this.time == -1) {
          this.time = 60;
          clearInterval(timer);
        }
      }, 1000);
      // request.post("/sendSMS", `phone=${this.phone}`).then((res) => {
      //   console.log(1111);
      //   console.log(res);
      // });
      //发送请求
      getVerifyApi(this.phone).then((res) => {
        // console.log(res);
        if (res.code === 0) {
          toas("验证码发送成功,请稍后", "success");
        }
      });
    },
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "验证成功, 耗时 " + ms + "s";
      this.isSlideVerfiy = true;
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
  },
};
</script>
 
<style lang = "less" scoped>
.login {
  background-color: rgba(0, 0, 0, 0.5);
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  .gaizu {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
  }
  .login-box {
    box-sizing: border-box;
    width: 555px;
    height: 423px;
    background-image: url("../../assets/images/login/loginBg.png");
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 40px 80px;
    .close {
      position: absolute;
      top: 20px;
      right: 60px;
      cursor: pointer;
    }
    .login-type {
      justify-content: center;
      li {
        padding: 0 10px;
        cursor: pointer;
        color: #cccc;
        &.active {
          color: #333;
        }
        &:nth-child(2) {
          border-left: 1px solid #ccc;
        }
      }
    }
    .phone-login {
      padding-top: 20px;
      .form-item {
        margin-bottom: 20px;
        display: flex;
        input {
          box-sizing: border-box;
          padding: 0 10px;
          height: 50px;
          border: 1px solid #ccc;
          flex: 1;
        }
        .btn {
          color: #fff;
          width: 120px;
          height: 50px;
          text-align: center;
          line-height: 50px;
          background-color: #0a328e;
          margin-left: 10px;
          cursor: pointer;
          &.big {
            width: 100%;
            margin: 0;
          }
        }
      }
    }
    .wx-login {
      img {
        width: 250px;
        height: 300px;
        margin: 30px 0 0 70px;
      }
    }
  }
}
/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -140px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -140px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
</style>